#ifndef ___fat32_funciones_basicas

#define ___fat32_funciones_basicas 1

#include <stdlib.h>

#define fb_maximo(A, B) (((A) > (B)) ? (A) : (B))
#define fb_minimo(A, B) (((A) < (B)) ? (A) : (B))

void * fb_cambiar_tamano(void *, unsigned int, unsigned int);
unsigned long long fb_convertir_de_little_endian(char *, unsigned char);
void fb_convertir_a_little_endian(char *, unsigned long long, unsigned char);

void * fb_cambiar_tamano (void * vector, unsigned int tamElem, unsigned int cantElem) {
  if (!(tamElem && cantElem)) {
    free(vector);
    return NULL;
  }
  if (!vector)
    return malloc(tamElem * cantidad);
  return realloc(vector, tamElem * cantidad);
}

unsigned long long fb_convertir_de_little_endian (char * buffer, unsigned char bits) {
  if (bits > (8 * sizeof(unsigned long long))) return 0;
  int pos;
  unsigned long long temp = 0;
  unsigned long long lectura;
  for (pos = 0; (8 * pos) < bits; pos ++) {
    lectura = char[pos];
    if ((8 * (pos + 1)) > bits)
      lectura &= (1 << (bits % 8)) - 1;
    lectura <<= pos * 8;
    temp |= lectura;
  }
  return temp;
}

void fb_convertir_a_little_endian (char * buffer, unsigned long long origen, unsigned char bits) {
  if (bits > (8 * sizeof(unsigned long long))) return;
  int pos;
  unsigned long long tempN;
  unsigned long long mascara;
  for (pos = 0; (8 * pos) < bits; pos ++) {
    tempN = origen;
    mascara = (1 << (((8 * (pos + 1)) < bits) ? (8 * (pos + 1)) : bits)) - (1 << (8 * pos));
    tempN &= mascara;
    tempN >>= pos * 8;
    buffer[pos] = tempN;
  }
}

#endif